package cn.fl.project.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.prjsteprebate.service.IPrjStepRebateApiService;
import cn.fl.project.api.IPrjRbtbillBscInfoApiService;
import cn.fl.project.condition.PrjRbtbillBscInfoQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.dto.PrjRbtbillBscInfoDTO;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.project.service.IPrjRbtbillBscInfoService;
import cn.fl.project.transform.PrjRbtbillBscInfoVOTransform;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdPaySchMVO;
import cn.fl.project.vo.PrjRbtbillBscInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 返利票据基础信息表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class PrjRbtbillBscInfoServiceImpl implements IPrjRbtbillBscInfoService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(PrjRbtbillBscInfoServiceImpl.class);
	
	/**
	 * 返利票据基础信息表 暴露服务
	 */
	@Autowired
	private IPrjRbtbillBscInfoApiService prjRbtbillBscInfoApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISysOrgApiService sysOrgApiService;

    @Autowired
    private IPrjStepRebateApiService iPrjStepRebateApiService;

    @Autowired
    private IPrjPrdPaySchMService prjPrdPaySchMService;
    @Autowired
    private IPrjBscInfoService prjBscInfoService;

	@Override
	public PrjRbtbillBscInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		PrjRbtbillBscInfoDTO dto = this.prjRbtbillBscInfoApiService.selectPrjRbtbillBscInfoById(id);
		return PrjRbtbillBscInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjRbtbillBscInfoVO entity) throws BaseException{
		if(null == entity) return null;
		return this.prjRbtbillBscInfoApiService.addPrjRbtbillBscInfo(PrjRbtbillBscInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.prjRbtbillBscInfoApiService.removePrjRbtbillBscInfoByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(PrjRbtbillBscInfoVO entity) throws BaseException{
		if(null == entity) return false;
		return this.prjRbtbillBscInfoApiService.modifyPrjRbtbillBscInfo(PrjRbtbillBscInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
        PrjRbtbillBscInfoQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new PrjRbtbillBscInfoQueryCondition());
        }
        condition = (PrjRbtbillBscInfoQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.prjRbtbillBscInfoApiService.searchPrjRbtbillBscInfoListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<PrjRbtbillBscInfoVO> voList = PrjRbtbillBscInfoVOTransform.toVOList((List<PrjRbtbillBscInfoDTO>) page.getResultList());
            List<String> paySchNos = new ArrayList<>(voList.size());
            Map<String, String> prjTypCdMap = this.sysDictionaryApiService.searchGroupToMapAll(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), "PRJ0002");
            for (PrjRbtbillBscInfoVO vo : voList) {
                PrjPrdPaySchMVO prjPrdPaySchMVO = this.prjPrdPaySchMService.selectById(vo.getPaySchId());
                if (prjPrdPaySchMVO != null) {
                    PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(prjPrdPaySchMVO.getPrjId());
                    if (prjBscInfoVO != null) {
                        vo.setPrjTypCd(prjBscInfoVO.getPrjTypCd());
                        vo.setBuOrgCd(prjBscInfoVO.getBuOrgCd());
                        vo.setFundRasInsNm(prjBscInfoVO.getFundRasInsNm());
                    }
                }
                if (prjTypCdMap != null && prjTypCdMap.containsKey(vo.getPrjTypCd())) {
                    vo.setPrjTypNm(prjTypCdMap.get(vo.getPrjTypCd()));
                }
                SysOrgDTO buOrgCd = this.sysOrgApiService.selectSysOrgByOrgCd(vo.getBuOrgCd(), CurrentThreadContext.getCurrentSysCd());
                vo.setBuOrgNm(buOrgCd.getOrgDispNm());
                if (PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(vo.getBuOrgCd())) {
                    paySchNos.add(vo.getPaySchNo());
                }
            }
            if (CollectionUtils.isNotEmpty(paySchNos)) {
                Map<String, Long> stepRebateFeeByPaySchNos = this.iPrjStepRebateApiService.getStepRebateFeeByPaySchNos(paySchNos);
                for (PrjRbtbillBscInfoVO prjRbtbillBscInfoVO : voList) {
                    if (stepRebateFeeByPaySchNos.containsKey(prjRbtbillBscInfoVO.getPaySchNo())) {
                        prjRbtbillBscInfoVO.setStepRebateAmt(stepRebateFeeByPaySchNos.get(prjRbtbillBscInfoVO.getPaySchNo()));
                    }
                }
            }
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }
	
	@Override
	public List<PrjRbtbillBscInfoVO> searchList(PrjRbtbillBscInfoQueryCondition condition) throws BaseException{
		if(null == condition) condition=new PrjRbtbillBscInfoQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<PrjRbtbillBscInfoVO> voList=PrjRbtbillBscInfoVOTransform.toVOList(this.prjRbtbillBscInfoApiService.searchPrjRbtbillBscInfoList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<PrjRbtbillBscInfoVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		Map<String, String> prjTypCdMap = this.sysDictionaryApiService.searchGroupToMapAll(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), "PRJ0002");
		for(PrjRbtbillBscInfoVO vo:voList){
			if (prjTypCdMap != null && prjTypCdMap.containsKey(vo.getPrjTypCd())) {
            	vo.setPrjTypNm(prjTypCdMap.get(vo.getPrjTypCd()));
        	}	
		}
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	@Override
	public Pager searchApplyListPage(Pager page) throws BaseException {
		PrjRbtbillBscInfoQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new PrjRbtbillBscInfoQueryCondition());
        }
		condition=(PrjRbtbillBscInfoQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.prjRbtbillBscInfoApiService.searchPaySchNoInfoListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<PrjRbtbillBscInfoVO> voList=PrjRbtbillBscInfoVOTransform.toVOList((List<PrjRbtbillBscInfoDTO>)page.getResultList());
            List<String> paySchNos = new ArrayList<>(voList.size());
            for ( PrjRbtbillBscInfoVO vo : voList ) {
                if (PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(vo.getBuOrgCd())) {
                    paySchNos.add(vo.getPaySchNo());
                }
                if(StringUtils.isNotEmpty(vo.getBuOrgCd())) {
                    SysOrgDTO buOrgCd = this.sysOrgApiService.selectSysOrgByOrgCd(vo.getBuOrgCd(), CurrentThreadContext.getCurrentSysCd());
                    vo.setBuOrgNm(buOrgCd.getOrgDispNm());
                }
            }
            if (CollectionUtils.isNotEmpty(paySchNos)) {
                Map<String, Long> stepRebateFeeByPaySchNos = this.iPrjStepRebateApiService.getStepRebateFeeByPaySchNos(paySchNos);
                for (PrjRbtbillBscInfoVO prjRbtbillBscInfoVO : voList) {
                    if (stepRebateFeeByPaySchNos.containsKey(prjRbtbillBscInfoVO.getPaySchNo())) {
                        prjRbtbillBscInfoVO.setStepRebateAmt(stepRebateFeeByPaySchNos.get(prjRbtbillBscInfoVO.getPaySchNo()));
                    }
                }
            }
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}

	@Override
	public List<PrjRbtbillBscInfoVO> searchApplyList(PrjRbtbillBscInfoQueryCondition qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		qc.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<PrjRbtbillBscInfoDTO> list = prjRbtbillBscInfoApiService.searchPaySchNoInfoList(qc);
		return PrjRbtbillBscInfoVOTransform.toVOList(list);
	}

	/**
	 * 根据申请ID删除报销明细
	 *
	 * @param applyId
	 * @return
	 * @throws BaseException
	 */
	@Override
	public boolean delByApplyId(Long applyId) throws BaseException {
		return this.prjRbtbillBscInfoApiService.delByApplyId(applyId);
	}
}
